package com.kcl.dfss;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.kcl.dfss.http.ServerConnection;
import com.kcl.dfss.utils.AppUtils;

public class ChangeNickActivity extends Activity implements OnClickListener{

	private EditText edit_nick;
	private ImageView clear_nick;
	private Button btn_nick;
	private LinearLayout changenick_back;
	private String usernick = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_change_nick);
		
		edit_nick = (EditText) findViewById(R.id.edit_nick);
		clear_nick = (ImageView) findViewById(R.id.clear_nick);
		btn_nick = (Button) findViewById(R.id.btn_nick);
		changenick_back = (LinearLayout) findViewById(R.id.changenick_back);
		
		clear_nick.setOnClickListener(this);
		changenick_back.setOnClickListener(this);
		btn_nick.setOnClickListener(this);
		btn_nick.setEnabled(false);
		
		edit_nick.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(edit_nick.getText().length()>0){
					btn_nick.setEnabled(true);
					btn_nick.setBackgroundResource(R.drawable.changepwd_selector);
				}else{
					btn_nick.setEnabled(false);
					btn_nick.setBackgroundResource(R.drawable.changepwd_noclick);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
		
		Intent intent = getIntent();
		usernick = intent.getStringExtra("usernick");
		edit_nick.setText(usernick);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.clear_nick:
			edit_nick.getText().clear();
			break;
		case R.id.btn_nick:
			final SharedPreferences preferences = getSharedPreferences("shared_login", 0);
			final String userid = preferences.getString("userid", "");
			final String nick = edit_nick.getText().toString();
			if(nick.replace(" ", "").equals("")){
				AppUtils.showDialog(ChangeNickActivity.this, "用户名不能为空");
			}else{
				new Thread(new Runnable() {
					@Override
					public void run() {
						String result = ServerConnection.UpdateNick(nick, userid);
						System.out.println(result);
						try {
							JSONObject jsonObject = new JSONObject(result);
							final String getresult = jsonObject.getString("success");
							runOnUiThread(new Runnable() {
								@Override
								public void run() {
									if(getresult.equals("true")){
										preferences.edit().putString("usernick", nick).commit();
										Toast.makeText(ChangeNickActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
										Intent intent = new Intent();
										intent.setClass(ChangeNickActivity.this, MainActivity.class);
										intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
										intent.putExtra("flag", 2);
										startActivity(intent);
										finish();
									}else{
										Toast.makeText(ChangeNickActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
									}
								}
							});
						} catch (JSONException e) {
							e.printStackTrace();
						} 
					}
				}).start();
			}
			break;
		case R.id.changenick_back:
			finish();
			break;
		}
	}
	
}
